package com.resource.exammanage.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.resource.common.utils.PageUtils;
import com.resource.common.utils.R;
import com.resource.exammanage.config.BeanToMapUtils;
import com.resource.exammanage.entity.TExamInformationDetailEntity;
import com.resource.exammanage.entity.TExamInformationEntity;
import com.resource.exammanage.feign.ExamSubmitRemote;
import com.resource.exammanage.service.TExamInformationDetailService;
import com.resource.exammanage.service.TExamInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 考试详细信息表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-04-04 17:00:38
 */
@RestController
@RequestMapping("exam/texaminformationdetail")
public class TExamInformationDetailController {
    @Autowired
    private TExamInformationDetailService tExamInformationDetailService;

    @Resource
    private ExamSubmitRemote examSubmitRemote;

    @Autowired
    TExamInformationService tExamInformationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tExamInformationDetailService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{examCode}")
    public R info(@PathVariable("examCode") String examCode){
		TExamInformationDetailEntity tExamInformationDetail = tExamInformationDetailService.getOne(new QueryWrapper<TExamInformationDetailEntity>().eq("exam_code",examCode));

        return R.ok().put("tExamInformationDetail", tExamInformationDetail);
    }

    /**
     * 保存/提交考试信息
     */
    @RequestMapping("/save")
    public R save(@RequestBody TExamInformationDetailEntity tExamInformationDetail){
		tExamInformationDetailService.save(tExamInformationDetail);
        Map<String, Object> stringMap = BeanToMapUtils.beanToMap(tExamInformationDetail);
        examSubmitRemote.submitNoticeByExam(stringMap);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TExamInformationDetailEntity tExamInformationDetail){
        tExamInformationDetailService.update(tExamInformationDetail,new QueryWrapper<TExamInformationDetailEntity>().eq("exam_code", tExamInformationDetail.getExamCode()));
        String examCode = tExamInformationDetail.getExamCode();
        TExamInformationEntity examSelect = tExamInformationService.getOne(new QueryWrapper<TExamInformationEntity>().eq("exam_code", examCode));
        //获取考试的学生信息
        String examStudentCode = examSelect.getExamStudentCode();
        String examStudentName = examSelect.getExamStudentName();
        //获取考试的教师信息
        String examTeacherCode = examSelect.getExamTeacherCode();
        String examTeacherName = examSelect.getExamTeacherName();
        String examContent = tExamInformationDetail.getExamContent();
        Map<String, Object> stringMap = BeanToMapUtils.beanToMap(tExamInformationDetail);
        stringMap.put("examStudentCode",examStudentCode);
        stringMap.put("examStudentName",examStudentName);
        stringMap.put("examTeacherCode",examTeacherCode);
        stringMap.put("examTeacherName",examTeacherName);
        stringMap.put("submitTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        stringMap.put("examContent",examContent);
        examSubmitRemote.submitNoticeByExam(stringMap);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] examCodes){
		tExamInformationDetailService.removeByIds(Arrays.asList(examCodes));

        return R.ok();
    }

}
